﻿ 
namespace Common.Algorithm.Core.LocalTest;
 
public class AnnealExecute
{
    static string assemblyPath = Assembly.GetExecutingAssembly().Location;
    static string assemblyDir = Path.GetDirectoryName(assemblyPath) + "\\";

    public static int[] Run(IEnumerable<GraphEdge> edges)
    {
        AnnealService anneal = new AnnealService(edges);
        Action action = () =>
        {
            anneal.RunAlgm(100000);
        };
        Console.WriteLine($"time:{TimeService.TimeWatch(action)}");

        Console.WriteLine($"{string.Join("-", anneal.bestPath)}");
        Console.WriteLine($"{AnnealService.PathLength(anneal.bestPath.ToArray())}");

        return anneal.bestPath.ToArray();
    }
    
    public static void RunMany(IEnumerable<GraphEdge> edges, int count)
    { 
        List<int[]> paths = new List<int[]>();
         
        Parallel.For(0, count,
           (item) =>
           {
               var finalPath = Run(edges);
               paths.Add(finalPath);
           }
           );
        var finalPath = paths.OrderBy(p => AnnealService.PathLength(p)).First(); 
        Console.WriteLine(
            $"BEST-DIST==={string.Join("\n", AnnealService.PathLength(finalPath.ToArray()))}");
    }

    public static void Execute()
    {
        string cityFile = @$"{assemblyDir}\_data_\pr76.txt";
        Run(GraphGenerator.GenCityGraph(cityFile, 70));
        RunMany(GraphGenerator.GenCityGraph(cityFile, 70),10);
    }

}
